package org.jeecg.modules.order.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.modules.common.CommonUtil;
import org.jeecg.modules.custom.entity.MtCustomEquip;
import org.jeecg.modules.custom.service.IMtCustomEquipService;
import org.jeecg.modules.engineer.mapper.MtEngineerMapper;
import org.jeecg.modules.equipment.entity.MtEquip;
import org.jeecg.modules.equipment.service.IMtEquipService;
import org.jeecg.modules.order.entity.*;
import org.jeecg.modules.order.mapper.*;
import org.jeecg.modules.order.service.IMtRepairOrderService;
import org.jeecg.modules.order.service.IMtRepairVisitService;
import org.jeecg.modules.order.vo.MtRepairOrderPage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工单表
 * @Author: jeecg-boot
 * @Date: 2022-09-26
 * @Version: V1.0
 */
@Service
public class MtRepairOrderServiceImpl extends ServiceImpl<MtRepairOrderMapper, MtRepairOrder> implements IMtRepairOrderService {

    @Autowired
    private MtRepairOrderMapper mtRepairOrderMapper;
    @Autowired
    private MtRepairDispatchMapper mtRepairDispatchMapper;
    @Autowired
    private MtRepairEquipMapper mtRepairEquipMapper;
    @Autowired
    private MtRepairScheduleMapper mtRepairScheduleMapper;
    @Autowired
    private MtRepairFeedbackMapper mtRepairFeedbackMapper;
    @Autowired
    private MtRepairDisposeMapper mtRepairDisposeMapper;
    @Autowired
    private MtEngineerMapper mtEngineerMapper;
    @Autowired
    private IMtCustomEquipService mtCustomEquipService;
    @Autowired
    private IMtEquipService mtEquipService;
    @Autowired
    private MtRepairVisitMapper mtRepairVisitMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(MtRepairOrder mtRepairOrder, List<MtRepairDispatch> mtRepairDispatchList,
                         List<MtRepairEquip> mtRepairEquipList, List<MtRepairSchedule> mtRepairScheduleList,
                         List<MtRepairFeedback> mtRepairFeedbackList, List<MtRepairDispose> mtRepairDisposeList) {
        mtRepairOrderMapper.insert(mtRepairOrder);
        if (mtRepairDispatchList != null && mtRepairDispatchList.size() > 0) {
            for (MtRepairDispatch entity : mtRepairDispatchList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairDispatchMapper.insert(entity);
            }
        }
        if (mtRepairEquipList != null && mtRepairEquipList.size() > 0) {
            for (MtRepairEquip entity : mtRepairEquipList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairEquipMapper.insert(entity);
            }
        }
        if (mtRepairScheduleList != null && mtRepairScheduleList.size() > 0) {
            for (MtRepairSchedule entity : mtRepairScheduleList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairScheduleMapper.insert(entity);
            }
        }
        if (mtRepairFeedbackList != null && mtRepairFeedbackList.size() > 0) {
            for (MtRepairFeedback entity : mtRepairFeedbackList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairFeedbackMapper.insert(entity);
            }
        }
        if (mtRepairDisposeList != null && mtRepairDisposeList.size() > 0) {
            for (MtRepairDispose entity : mtRepairDisposeList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairDisposeMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(MtRepairOrder mtRepairOrder, List<MtRepairDispatch> mtRepairDispatchList, List<MtRepairEquip> mtRepairEquipList, List<MtRepairSchedule> mtRepairScheduleList, List<MtRepairFeedback> mtRepairFeedbackList, List<MtRepairDispose> mtRepairDisposeList) {
        mtRepairOrderMapper.updateById(mtRepairOrder);

        //1.先删除子表数据
        mtRepairDispatchMapper.deleteByMainId(mtRepairOrder.getId());
        mtRepairEquipMapper.deleteByMainId(mtRepairOrder.getId());
        mtRepairScheduleMapper.deleteByMainId(mtRepairOrder.getId());
        mtRepairFeedbackMapper.deleteByMainId(mtRepairOrder.getId());
        mtRepairDisposeMapper.deleteByMainId(mtRepairOrder.getId());

        //2.子表数据重新插入
        if (mtRepairDispatchList != null && mtRepairDispatchList.size() > 0) {
            for (MtRepairDispatch entity : mtRepairDispatchList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairDispatchMapper.insert(entity);
            }
        }
        if (mtRepairEquipList != null && mtRepairEquipList.size() > 0) {
            for (MtRepairEquip entity : mtRepairEquipList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairEquipMapper.insert(entity);
            }
        }
        if (mtRepairScheduleList != null && mtRepairScheduleList.size() > 0) {
            for (MtRepairSchedule entity : mtRepairScheduleList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairScheduleMapper.insert(entity);
            }
        }
        if (mtRepairFeedbackList != null && mtRepairFeedbackList.size() > 0) {
            for (MtRepairFeedback entity : mtRepairFeedbackList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairFeedbackMapper.insert(entity);
            }
        }
        if (mtRepairDisposeList != null && mtRepairDisposeList.size() > 0) {
            for (MtRepairDispose entity : mtRepairDisposeList) {
                //外键设置
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairDisposeMapper.insert(entity);
            }
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void repairOrderDispatch(MtRepairOrder mtRepairOrder, MtRepairDispatch mtRepairDispatch, MtRepairSchedule mtRepairSchedule) {
        mtRepairOrderMapper.updateById(mtRepairOrder);

        // 子表数据插入
        if (mtRepairDispatch != null) {
            //外键设置
            mtRepairDispatch.setOrderId(mtRepairOrder.getId());
            mtRepairDispatchMapper.insert(mtRepairDispatch);
        }
        if (mtRepairSchedule != null) {
            //外键设置
            mtRepairSchedule.setOrderId(mtRepairOrder.getId());
            mtRepairScheduleMapper.insert(mtRepairSchedule);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByWork(MtRepairOrderPage mtRepairOrderPage) {
        MtRepairOrder mtRepairOrder = new MtRepairOrder();
        BeanUtils.copyProperties(mtRepairOrderPage, mtRepairOrder);
        mtRepairOrderMapper.updateById(mtRepairOrder);

        // 派工记录
        List<MtRepairDispatch> mtRepairDispatchList = mtRepairOrderPage.getMtRepairDispatchList();
        if (mtRepairDispatchList != null && mtRepairDispatchList.size() > 0) {
            for (MtRepairDispatch entity : mtRepairDispatchList) {
                if (StrUtil.isBlank(entity.getId())) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairDispatchMapper.insert(entity);
                }
                mtRepairDispatchMapper.updateById(entity);
            }
        }
        // 设备记录
        List<MtRepairEquip> mtRepairEquipList = mtRepairOrderPage.getMtRepairEquipList();
        if (mtRepairEquipList != null) {
            MtRepairSchedule repairSchedule = mtRepairOrderPage.getMtRepairScheduleList().get(0);
            mtRepairEquipMapper.delete(new LambdaUpdateWrapper<MtRepairEquip>()
                    .eq(MtRepairEquip::getOrderId, mtRepairOrder.getId())
                    .eq(MtRepairEquip::getEngineerId, repairSchedule.getEngnieerId()));
            if (mtRepairEquipList.size() > 0) {
                for (MtRepairEquip entity : mtRepairEquipList) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairEquipMapper.insert(entity);
                }
            }
        }
        // 进度记录
        List<MtRepairSchedule> mtRepairScheduleList = mtRepairOrderPage.getMtRepairScheduleList();
        if (mtRepairScheduleList != null && mtRepairScheduleList.size() > 0) {
            for (MtRepairSchedule entity : mtRepairScheduleList) {
                if (StrUtil.isBlank(entity.getId())) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairScheduleMapper.insert(entity);
                }
                mtRepairScheduleMapper.updateById(entity);
            }
        }
        // 评价记录
        List<MtRepairFeedback> mtRepairFeedbackList = mtRepairOrderPage.getMtRepairFeedbackList();
        if (mtRepairFeedbackList != null && mtRepairFeedbackList.size() > 0) {
            for (MtRepairFeedback entity : mtRepairFeedbackList) {
                if (StrUtil.isBlank(entity.getId())) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairFeedbackMapper.insert(entity);
                }
                mtRepairFeedbackMapper.updateById(entity);
            }
        }
        // 处理记录
        List<MtRepairDispose> mtRepairDisposeList = mtRepairOrderPage.getMtRepairDisposeList();
        if (mtRepairDisposeList != null && mtRepairDisposeList.size() > 0) {
            for (MtRepairDispose entity : mtRepairDisposeList) {
                if (StrUtil.isBlank(entity.getId())) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairDisposeMapper.insert(entity);
                }
                mtRepairDisposeMapper.updateById(entity);
            }
        }
        // 回访记录
        List<MtRepairVisit> mtRepairVisitList = mtRepairOrderPage.getMtRepairVisitList();
        if (mtRepairVisitList != null && mtRepairVisitList.size() > 0) {
            for (MtRepairVisit entity : mtRepairVisitList) {
                if (StrUtil.isBlank(entity.getId())) {
                    entity.setOrderId(mtRepairOrder.getId());
                    mtRepairVisitMapper.insert(entity);
                }
                mtRepairVisitMapper.updateById(entity);
            }
        }
        // 客户设备业务更新
        if (mtRepairOrder.getUpdateCustomEquip() != null && mtRepairOrder.getUpdateCustomEquip()) {
            List<MtRepairEquip> repairEquipList = mtRepairEquipMapper.selectByMainId(mtRepairOrder.getId());
            for (MtRepairEquip repairEquip : repairEquipList) {
                if ("0".equals(repairEquip.getDisposeStyle())) {
                    mtCustomEquipService.repair(repairEquip.getCustomEquipId());
                } else if ("1".equals(repairEquip.getDisposeStyle())) {
                    mtCustomEquipService.replace(repairEquip);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void repairOrderCancelDispatch(MtRepairOrderPage mtRepairOrderPage, List<MtRepairDispatch> deleteDispatch) {
        MtRepairOrder mtRepairOrder = new MtRepairOrder();
        BeanUtils.copyProperties(mtRepairOrderPage, mtRepairOrder);

        // 删除指定派工记录
        if (deleteDispatch != null && deleteDispatch.size() > 0) {
            for (MtRepairDispatch entity : deleteDispatch) {
                mtRepairDispatchMapper.deleteById(entity);
                // 删除指定设备记录
                mtRepairEquipMapper.delete(new LambdaUpdateWrapper<MtRepairEquip>()
                        .eq(MtRepairEquip::getEngineerId, entity.getEngineerId()));
                // 删除指定处理记录
                mtRepairDisposeMapper.delete(new LambdaUpdateWrapper<MtRepairDispose>()
                        .eq(MtRepairDispose::getEngineerId, entity.getEngineerId()));
            }
        }
        // 更新指定派工记录
        List<MtRepairDispatch> mtRepairDispatchList = mtRepairOrderPage.getMtRepairDispatchList();
        if (mtRepairDispatchList != null && mtRepairDispatchList.size() > 0) {
            for (MtRepairDispatch entity : mtRepairDispatchList) {
                mtRepairDispatchMapper.updateById(entity);
            }
        }
        // 插入进度跟踪
        List<MtRepairSchedule> mtRepairScheduleList = mtRepairOrderPage.getMtRepairScheduleList();
        if (mtRepairScheduleList != null && mtRepairScheduleList.size() > 0) {
            for (MtRepairSchedule entity : mtRepairScheduleList) {
                entity.setOrderId(mtRepairOrder.getId());
                mtRepairScheduleMapper.insert(entity);
            }
        }

        // 更新工单状态
        List<MtRepairDispatch> repairDispatchList = mtRepairDispatchMapper.selectByMainId(mtRepairOrder.getId());
        if (repairDispatchList.size() <= 0) {
            mtRepairOrder.setProcessingStatus("0");//已受理
        }
        // 更新主表数据
        mtRepairOrderMapper.updateById(mtRepairOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        mtRepairDispatchMapper.deleteByMainId(id);
        mtRepairEquipMapper.deleteByMainId(id);
        mtRepairScheduleMapper.deleteByMainId(id);
        mtRepairFeedbackMapper.deleteByMainId(id);
        mtRepairDisposeMapper.deleteByMainId(id);
        mtRepairOrderMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            mtRepairDispatchMapper.deleteByMainId(id.toString());
            mtRepairEquipMapper.deleteByMainId(id.toString());
            mtRepairScheduleMapper.deleteByMainId(id.toString());
            mtRepairFeedbackMapper.deleteByMainId(id.toString());
            mtRepairDisposeMapper.deleteByMainId(id.toString());
            mtRepairOrderMapper.deleteById(id);
        }
    }

    @Override
    public List<MtRepairOrder> getProcessingListByEngineer(String engineerId) {
        return mtRepairOrderMapper.getProcessingListByEngineer(engineerId);
    }

}
